package com.zhang.zhangapp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.HttpConfig;
import com.zhang.zhangapp.bean.ZFile;
import com.zhang.zhangapp.utils.DensityUtils;
import com.zhang.zhangapp.utils.ScreenUtils;

import org.w3c.dom.Text;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by 罗肖 on 2018/4/11.
 */

public class ShopCharacteristicAdapter extends BaseAdapter {

    List<ZFile> fileList;
    Context context;
    LinearLayout.LayoutParams layoutParams;

    public ShopCharacteristicAdapter(List<ZFile> fileList, Context context) {
        this.fileList = fileList;
        this.context = context;
        int size = (ScreenUtils.getScreenWidth(context) - DensityUtils.dp2px(context,16*5 +50)) / 4;
        layoutParams = new LinearLayout.LayoutParams(size, size);
    }

    @Override
    public int getCount() {
        if (fileList == null) {
            return 0;
        } else if (fileList.size() >= 4) {
            return 4;
        } else {
            return fileList.size();
        }

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_shop_characteristic, null);
            x.view().inject(viewHolder, view);
            view.setTag(viewHolder);
        } else viewHolder = (ViewHolder) view.getTag();
        String[] str = fileList.get(position).getPicurl().split("~~");
        viewHolder.imageView.setLayoutParams(layoutParams);
        x.image().bind(viewHolder.imageView, str[0], MApplication.getInstance().getImageOptions());
        if (str.length > 1)
            viewHolder.textView.setText(str[1]);
        else
            viewHolder.textView.setText("");
        viewHolder.textView.setText(fileList.get(position).getName());
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.item_shop_characteristic_img)
        ImageView imageView;
        @ViewInject(R.id.item_shop_characteristic_name)
        TextView textView;
    }
}
